home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10460 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  100 lines

  1. Path: news.cc.sunysb.edu!ghauser
  2. From: ghauser@ic.sunysb.edu (George Hauser)
  3. Newsgroups: comp.lang.c
  4. Subject: Need help with simple linked list... I'm stuck!!!
  5. Date: 18 Mar 1996 00:53:28 GMT
  6. Organization: State University of New York at Stony Brook
  7. Message-ID: <4iic68$jkc@abel.cc.sunysb.edu>
  8. NNTP-Posting-Host: engws12.cc.sunysb.edu
  9.  
  10. Hi, the following code is supposed to do simple insertions into a queue
  11. and then print its contents.
  12.  
  13. The same logic works fine when creating a class using C++, but here
  14. it seems that I am not passing the arguments to add_node by reference.
  15.  
  16. I think I am... I really don't see whats wrong with this. If someone can
  17. suggest something please let me know.
  18.  
  19. I am using Borland's C++ 4.52 compiler under Win95.
  20.  
  21. Thanks for your help!
  22.  
  23.  
  24. - George
  25.  
  26.  
  27. --------
  28.  
  29. /* Standard library includes */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32.  
  33.  
  34. struct a_node    /* The record that contains the information and the pointer */
  35. {
  36.     char data;
  37.     struct a_node *next;
  38. };
  39.  
  40. /* Redifine the node data type */
  41. typedef struct a_node node;
  42.  
  43. /* this is supposed to initialize and return the pointers to the current list. */
  44.  
  45. void add_node(char c, node *tmphead, node *tmptail)
  46. {
  47. node *temp;
  48.     temp = (node *) malloc(sizeof(node));  /* Allocate memory for new node */
  49.  
  50.     /* Initialize the new node */
  51.     temp->data = c;
  52.     temp->next = NULL;
  53.  
  54.     /* Make the correct pointer manipulations */
  55.     if (tmphead == NULL)        //No nodes in list
  56.     {
  57.         tmphead = temp;
  58.         tmptail = temp;
  59.     }
  60.     else
  61.     {
  62.         tmptail->next = temp;  //Simply link the new nodes.
  63.         tmptail = temp;
  64.     }
  65. }
  66.  
  67. void print(node *tmphead)
  68. {
  69. node *tmp;
  70.  
  71.     tmp = tmphead;
  72.     if (tmp == NULL) printf("The tmphead is NULL!!\n");
  73.     while(tmp != NULL)
  74.     {
  75.         printf("%c",tmp->data);        /*Print the character in the current node */
  76.         tmp = tmp->next;
  77.         printf("Moved once, twice.\n");
  78.     }
  79. }
  80.  
  81.  
  82. main()
  83. {
  84. node *head, *tail;
  85. char letter;
  86.  
  87.     head = NULL;
  88.     tail = NULL;
  89.     printf("Please enter a string: ");
  90.     while((letter = getchar()) != '\n')
  91.     {
  92.         printf("In the while letter != \n");  //Debug statement.
  93.         add_node(letter,head,tail);
  94.     }
  95.     printf("Out side of the while loop! \n");
  96.     print(head);
  97. }
  98.  
  99.  
  100.